<template>
  <view class="register-container">
    <view class="register-card">
      <view class="register-title">注册配送员账号</view>
      <view class="input-row">
        <view class="input-label">请输入姓名</view>
        <uni-easyinput class="register-input" :class="{'input-error-border': nameError}" type="text" v-model="name" placeholder="输入姓名" :placeholder-style="placeholderStyle" :inputBorder="false" :focus="focusField==='name'" @focus="focusField='name'" @blur="onBlur('name')" />
      </view>
      <view v-if="nameError" class="input-error">请输入姓名</view>
      <view style="height: 18px;"></view>
      <view class="input-row">
        <view class="input-label">请输入账号</view>
        <uni-easyinput class="register-input" :class="{'input-error-border': accountError}" type="text" v-model="account" placeholder="输入账号" :placeholder-style="placeholderStyle" :inputBorder="false" :focus="focusField==='account'" @focus="focusField='account'" @blur="onBlur('account')" />
      </view>
      <view v-if="accountError" class="input-error">请输入账号</view>
      <view style="height: 18px;"></view>
      <view class="input-row">
        <view class="input-label">请输入密码</view>
        <uni-easyinput class="register-input" :class="{'input-error-border': passwordError}" type="password" v-model="password" placeholder="设置密码" :placeholder-style="placeholderStyle" :inputBorder="false" :focus="focusField==='password'" @focus="focusField='password'" @blur="onBlur('password')" />
      </view>
      <view v-if="passwordError" class="input-error">请输入密码</view>
      <view style="height: 18px;"></view>
      <view class="input-row">
        <view class="input-label">请输入电话</view>
        <uni-easyinput class="register-input" :class="{'input-error-border': phoneError || phoneFormatError}" type="number" v-model="phone" placeholder="输入电话" :placeholder-style="placeholderStyle" :inputBorder="false" :focus="focusField==='phone'" @focus="focusField='phone'" @blur="onBlur('phone')" :maxlength="11" @input="onPhoneInput" />
      </view>
      <view v-if="phoneError" class="input-error">请输入电话</view>
      <view v-if="phoneFormatError" class="input-error">电话格式不正确，只能输入数字</view>
      <button 
        class="register-btn" 
        :disabled="!name || !account || !password || !phone" 
        @click="handleRegister"
      >
        注册
      </button>
    </view>
  </view>
</template>

<script>
import { request } from '@/utils/request'

export default {
  data() {
    return {
      name: '',
      account: '',
      password: '',
      phone: '',
      placeholderStyle: 'font-size:18px;color:#bcbcbc;',
      focusField: '',
      nameError: false,
      accountError: false,
      passwordError: false,
      phoneError: false,
      phoneFormatError: false
    }
  },
  methods: {
    onBlur(field) {
      if (field === 'name') {
        this.nameError = !this.name;
      } else if (field === 'account') {
        this.accountError = !this.account;
      } else if (field === 'password') {
        this.passwordError = !this.password;
      } else if (field === 'phone') {
        this.phoneError = !this.phone;
      }
      this.focusField = '';
    },
    onPhoneInput(val) {
      // 检查是否有非数字字符
      if (/\D/.test(val)) {
        this.phoneFormatError = true;
      } else {
        this.phoneFormatError = false;
      }
      // 只保留数字，最大11位
      this.phone = val.replace(/\D/g, '').slice(0, 11);
    },
    async handleRegister() {
      // 检查是否有非数字字符
      if (/\D/.test(this.phone)) {
        this.phoneFormatError = true;
        return;
      }

      // 验证姓名是否为空
      this.nameError = !this.name;
      
      // 验证账号是否为空
      this.accountError = !this.account;
      
      // 验证密码是否为空
      this.passwordError = !this.password;
      
      // 验证电话是否为空
      this.phoneError = !this.phone;

      // 如果任何一项验证失败，阻止表单提交
      if (this.nameError || this.accountError || this.passwordError || this.phoneError) {
        return;
      }

      try {
        const res = await request({
          url: '/courier/register',
          method: 'POST',
          data: {
            courierName: this.name,
            courierAccount: this.account,
            courierPassword: this.password,
            phone: this.phone
          }
        });

        console.log('注册响应:', res);

        if (res.code === 200) {
          uni.showToast({
            title: '注册成功',
            icon: 'success',
            duration: 2000
          });

          setTimeout(() => {
            uni.redirectTo({
              url: `/pages/login/courierLogin`
            });
          }, 2000);
        } else {
          uni.showToast({
            title: res.message || '注册失败',
            icon: 'none',
            duration: 2000
          });
        }
      } catch (err) {
        console.error('注册请求失败:', err);
        uni.showToast({
          title: '网络错误，请稍后重试',
          icon: 'none',
          duration: 2000
        });
      }
    }
  }
}
</script>

<style>
.register-container {
  min-height: 100vh;
  background: #f8f8f8;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
.register-card {
  background: #fff;
  border-radius: 18px;
  box-shadow: 0 4px 24px #e0e0e0;
  padding: 40px 28px 32px 28px;
  width: 90%;
  max-width: 400px;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.register-title {
  font-size: 26px;
  font-weight: bold;
  margin-bottom: 36px;
  margin-top: 0;
  color: #222;
}
.input-row {
  width: 100%;
  display: flex;
  align-items: center;
  margin-bottom: 0;
}
.input-label {
  min-width: 90px;
  font-size: 17px;
  color: #888;
  margin-right: 12px;
  text-align: left;
}
.register-input {
  flex: 1;
  margin: 0;
  --easyinput-border-radius: 10px;
  --easyinput-border-color: #e0e0e0;
  --easyinput-border-width: 1.5px;
  --easyinput-background-color: #fff;
  --easyinput-placeholder-color: #bcbcbc;
  --easyinput-font-size: 18px;
  --easyinput-height: 48px;
  --easyinput-padding: 0 16px;
  --easyinput-color: #222;
  box-shadow: none;
}
.input-error-border /deep/ .uni-easyinput__content {
  border-color: #e74c3c !important;
}
/deep/ .uni-easyinput__content {
  border-radius: 10px !important;
  border: 1.5px solid #e0e0e0 !important;
  background: #fff !important;
  transition: border-color 0.2s;
  box-shadow: none !important;
}
/deep/ .uni-easyinput__content.is-focused {
  border-color: #007aff !important;
}
/deep/ .uni-easyinput__placeholder {
  color: #bcbcbc !important;
  font-size: 18px !important;
}
.input-error {
  width: 100%;
  color: #e74c3c;
  font-size: 14px;
  margin: 4px 0 0 102px;
  text-align: left;
}
.register-btn {
  width: 100%;
  margin: 32px 0 0 0;
  background: #007aff;
  color: #fff;
  font-size: 19px;
  border-radius: 10px;
  padding: 14px 0;
  letter-spacing: 1px;
  box-shadow: 0 2px 8px #e0e0e0;
}
</style> 